<template>
</template>

<script>
	import {
		request,
		requestData
	} from '@/utils/http.js'
	export default {
		data() {
			return {
				payOption: ''
			}
		},
		onLoad(option) {
			if (option) {
				console.log('支付参数：', option)
				this.payOption = option;
				this.template_id = option.template_id
			}
		},
		onShow() {
			this.fnOrder()
		},
		methods: {
			//下订单
			fnOrder() {
				request('/user/ticket/buy/wx', {
						session_id: this.payOption.session_id,
						specified_date: this.payOption.specified_date,
					})
					.then((res) => {
						console.log(res)
						if (res.code == 0) {
							// uni.removeStorageSync('appId')
							// uni.removeStorageSync('timeStamp')
							// uni.removeStorageSync('nonceStr')
							// uni.removeStorageSync('package')
							// uni.removeStorageSync('signType')
							// uni.removeStorageSync('paySign')
							// uni.setStorageSync('appId', res?.datas?.appId || '')
							// uni.setStorageSync('timeStamp', res?.datas?.timeStamp || '')
							// uni.setStorageSync('nonceStr', res?.datas?.nonceStr || '')
							// uni.setStorageSync('package', res?.datas?.package || '')
							// uni.setStorageSync('signType', res?.datas?.signType || '')
							// uni.setStorageSync('paySign', res?.datas?.paySign || '')
							// uni.setStorageSync('ticket_id', res?.datas?.ticket_id || '')
							let payData = res?.datas;
							this.fnWxPay(payData) //支付
						} else if (res.code == '-57026') {
							uni.showToast({
								title: '请先完善个人信息',
								icon: 'none',
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '/pagesC/myInfo/index',
								})
							}, 2000)
						} else if (res.code == '-57022') {
							uni.showToast({
								title: '请先上传人脸照片',
								icon: 'none',
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '/pagesC/myInfo/index',
								})
							}, 2000)
						} else if (res.code == '-57472') {
							uni.showToast({
								title: '该泳票已购买过',
								icon: 'none',
							})
							setTimeout(() => {
								uni.redirectTo({
									url: `/pagesB/titckDetail/index?id=${this.template_id}`,
								})
							}, 2000)
						}
					})
					.catch((err) => {
						console.log(err)
					})

			},

			fnWxPay(payData) {
				let that = this
				wx.requestPayment({
					timeStamp: payData.timeStamp, //时间戳
					nonceStr: payData.nonceStr, //随机字符串
					package: payData.package, //prepay_id
					signType: payData.signType, //签名算法MD5
					paySign: payData.paySign, //签名
					success(res) {
						if (res.errMsg == 'requestPayment:ok') {
							uni.showToast({
								title: '支付成功！',
								icon: 'success',
							})
							setTimeout(() => {
								uni.redirectTo({
									url: `/pagesB/titckDetail/index?id=${that.template_id}`,
								})
							}, 2000)
						} else {
							uni.showToast({
								title: '支付失败',
								icon: 'error',
							})
						}
					},
					fail(res) {
						if (res.errMsg == 'requestPayment:fail cancel') {
							that.fnCancelPay(payData) //取消支付,支付失败
							setTimeout(() => {
								uni.redirectTo({
									url: `/pagesB/titckDetail/index?id=${that.template_id}`,
								})
							}, 2000)

						}
					},
					complete(res) {
						console.log(res)
					}
				})
			},
			//取消支付,支付失败
			fnCancelPay(payData) {
				request('/user/ticket/pay/cancel', {
						ticket_id: payData.ticket_id
					})
					.then((res) => {
						console.log(res)
						uni.showToast({
							title: '已取消支付',
							icon: 'success',
						})
					})
					.catch((err) => {
						console.log(err)
					})
			}
		}
	}
</script>
<style></style>